﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using RooHui.Infrastructure.Entities.Entity;
using RooHui.Tool;
using RooHui.Infrastructure.Entities.Enum;

namespace RooHui.OA.Biz
{
    public class RoleBiz
    {
        /// <summary>
        /// 获取所有用户信息
        /// </summary>
        /// <returns></returns>
        public static List<Role> GetRoleList()
        {
            try
            {
                var roleList = new Role().FindAll(c => c.Status != (int)ItemStatus.Delete);
                return roleList;
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        public static bool RoleExsit(string roleName,int roleID=0)
        {
            bool isTrue = false;
            try
            {
                var role = new Role().Find(c => c.Name == roleName && c.Status != (int)ItemStatus.Delete && c.ID != roleID);
                if (role != null)
                {
                    isTrue = true;
                }
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return isTrue;
        }

        public static Role RoleSave(string roleName, int roleID = 0, string createUser = "")
        {
            try
            {
                var role = new Role();
                if (roleID == 0)
                {
                    role.Name = roleName;
                    role.Status = ItemStatus.Enable;
                    role.CreateTime = DateTime.Now;
                    role.CreateUser = createUser;
                    role=role.PostAdd();
                }
                else
                {
                    role = role.Find(roleID.ToString());
                     role.Name = roleName; 
                     role = role.PostModify();
                }
                return role;
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }

        public static Role DeleteRole(int roleID)
        {
            try
            {
                var role = new Role().Find(roleID.ToString());
                role.Status = ItemStatus.Delete;
                role = role.PostModify();
                RoleModuleBiz.RoleAuthorizeDelete(roleID);
                return role;
            }
            catch (Exception ex)
            {
                LogHelper.Write(CommonLogger.Web, ex);
            }
            return null;
        }
    }
}
